﻿
using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleGridView {

	[PersistenceMode(PersistenceMode.InnerProperty)]
	[Bindable(false)]
	[ToolboxItem(false)]
	//[PersistChildren(true)]
	[ParseChildren(true)]
	//[TypeConverter(typeof(ExpandableObjectConverter))]
	[ControlBuilder(typeof(ColumnBuilder))]
	public abstract class SpGvBaseColumn {
		public class ColumnBuilder : ControlBuilder {
			public override bool AllowWhitespaceLiterals() {
				return true;
			}
			public override void AppendLiteralString(string s) {
			}
		}

	
		protected SpGvBaseColumn() {
			ContentStyle = new TableCellStyle();
			HeaderStyle = new TableCellStyle();
			HeaderExportStyle = new TableCellStyle();
			HeaderExportStyle.BackColor = Color.Gray;
			HeaderExportStyle.ForeColor = Color.White;
			HeaderExportStyle.HorizontalAlign = HorizontalAlign.Center;
			HeaderExportStyle.VerticalAlign = VerticalAlign.Middle;
			HeaderExportStyle.Font.Bold = true;
			ContentExportStyle = new TableCellStyle();
			Visible = true;
			EnableSort = true;
			Filter = FilterType.None;
			EditValueMode = EditValueMode.InnerHtml;
			EditState = EditState.AddAndEdit;
			FieldType = DataFieldType.Text;
		}
		public string DisplayFormat { get; set; }
		private string _name;
		public string Name {
			get {
				if (_name == null) return FieldName;
				return _name;
			}
			set { _name = value; }
		}
		public string Caption { get; set; }
		public string FieldName { get; set; }
		public EditValueMode EditValueMode { get; set; }
		public EditState EditState { get; set; }
		[Description("")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public TableCellStyle HeaderStyle { get; private set; }
		[Description("")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public TableCellStyle HeaderExportStyle { get; private set; }
		[Description("")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public TableCellStyle ContentExportStyle { get; private set; }

		[Description("")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public TableCellStyle ContentStyle { get; private set; }
		[Description("")]
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]

		public ITemplate HeaderTemplate { get; set; }

		[PersistenceMode(PersistenceMode.InnerProperty)]
		[TemplateContainer(typeof(SpGvCellItemTemplate))]
		[MergableProperty(false)]


		public ITemplate ItemTemplate { get; set; }

		public abstract GridColumnType ColumnType { get; }
		[Description("Field type of this column")]
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[DefaultValue(DataFieldType.Text)]
		public DataFieldType FieldType { get; set; }

		internal virtual BaseControl CreateClientEditControl() {
			return null;
		}

		internal void InsertValueToTableCell(SpGridView grid, SpGvCellItemTemplate cell, object value, object dataItem) {
			if (value != null) {
				cell.Controls.Add(new LiteralControl(InternalGetText(value, dataItem)));
			}
			if (grid.EditType == EditType.Inline && EditValueMode == EditValueMode.Attribute) {
				string strValue = InternalGetValue(value, dataItem);
				if (strValue != null) {
					cell.Value = strValue;
				}
			}
		}

		internal string InternalGetValue(object value, object dataItem) {
			var handler = CustomGetValue;
			if (handler != null) {
				var e = new CustomGetEventArgs(value, dataItem);
				handler(this, e);
				if (e.Handled) return e.Result;
			}
			return GetValueFromObject(value);
		}

		internal string InternalGetText(object value, object dataItem) {
			var handler = CustomGetDisplayText;
			if (handler != null) {
				var e = new CustomGetEventArgs(value, dataItem);
				handler(this, e);
				if (e.Handled) return e.Result;

			}
			return GetTextFromObject(value);
		}

		protected virtual string GetValueFromObject(object value) {
			return value == null ? String.Empty : value.ToString();
		}

		protected virtual string GetTextFromObject(object value) {
			string text;
			if (String.IsNullOrEmpty(DisplayFormat)) {
				text = value.ToString();
			} else {
				string formatText = "{0:" + DisplayFormat + "}";
				text = String.Format(formatText, value);
			}
			return text;
		}

		internal string EditValuePostedFromClientOnEditMode { get; set; }
		internal object InternalGetValuePostedFromClientOnEditMode() {
			var handler = CustomParseText;
			if (handler != null) {
				var e = new CustomParseTextEventArgs(EditValuePostedFromClientOnEditMode);
				handler(this, e);
				if (e.Handled) return e.Value;
			}
			return GetValuePostedFromClientOnEditMode();
		}
		protected virtual object GetValuePostedFromClientOnEditMode() {
			return EditValuePostedFromClientOnEditMode;
		}
		[Description("Get or set column visible")]
		[PersistenceMode(PersistenceMode.Attribute)]
		[MergableProperty(false)]
		[Category("Data")]
		[DefaultValue(true)]
		public bool Visible { get; set; }
		[Description("Get or set sortable column")]
		[PersistenceMode(PersistenceMode.Attribute)]
		[MergableProperty(false)]
		[Category("Data")]
		[DefaultValue(false)]
		public bool EnableSort { get; set; }
		[Description("Get or set column filter type. None: do not show filter for this column, Enter: show filter at client, and filter occur when user press enter key")]
		[PersistenceMode(PersistenceMode.Attribute)]
		[MergableProperty(false)]
		[Category("Data")]
		[DefaultValue(FilterType.None)]
		public FilterType Filter { get; set; }
		/// <summary>
		/// Get value when binding
		/// </summary>
		public event EventHandler<CustomGetEventArgs> CustomGetValue;
		/// <summary>
		/// Get display text when binding
		/// </summary>
		public event EventHandler<CustomGetEventArgs> CustomGetDisplayText;
		/// <summary>
		/// Get value from client posted text in inline edit mode
		/// </summary>
		public event EventHandler<CustomParseTextEventArgs> CustomParseText;


	}
}
